import 'package:date_format/date_format.dart';

/// FileName date_time_extension
///
/// @Author lax
/// @Date 2024/3/22
///
/// @Description

enum DateType {
  kYMDHMS,
  kYMDHM,
  kYMD,
  kyMD,
  kYM,
  kMDHM,
  kMDHMS,
  kMD,
  kHMS,
  kHM,
}

extension DateTypeFormats on DateType {
  List<String> dateFormats({String? separator}) {
    separator ??= '-';
    switch (this) {
      case DateType.kYMDHMS:
        return [yyyy, separator, mm, separator, dd, ' ', HH, ':', nn, ':', ss];
      case DateType.kYMDHM:
        return [yyyy, separator, mm, separator, dd, ' ', HH, ':', nn];
      case DateType.kYMD:
        return [yyyy, separator, mm, separator, dd];
      case DateType.kyMD:
        return [yy, separator, mm, separator, dd];
      case DateType.kYM:
        return [yyyy, separator, mm];
      case DateType.kMDHM:
        return [mm, separator, dd, ' ', HH, ':', nn];
      case DateType.kMDHMS:
        return [mm, separator, dd, ' ', HH, ':', nn, ':', ss];
      case DateType.kMD:
        return [mm, separator, dd];
      case DateType.kHMS:
        return [HH, ':', nn, ':', ss];
      case DateType.kHM:
        return [HH, ':', nn];
      default:
        return [];
    }
  }
}

extension DateTimeExtension on DateTime {
  /// 年月日(2020-02-02)
  String dateString({DateType? dateType = DateType.kYMD, String? separator}) {
    return dateTimeString(dateType: dateType, separator: separator);
  }

  /// 年月日(2020.02.02)
  String dateStringWithDot() {
    return dateTimeString(dateType: DateType.kYMD, separator: '.');
  }

  /// 格式化日期(2020-02-02 00:00:00)
  String dateTimeString({DateType? dateType = DateType.kYMDHMS, String? separator}) {
    return formatDate(this, dateType?.dateFormats(separator: separator) ?? []);
  }

  /// 格式化日期(2020.02.02 00:00:00)
  String dateTimeStringWithDot() {
    return formatDate(this, DateType.kYMDHMS.dateFormats(separator: '.'));
  }

  /// 0点时间
  DateTime dayZeroTime() {
    return DateTime(year, month, day);
  }

  /// 本年第一天
  DateTime yearFirstDay() {
    return DateTime(year);
  }

  /// 本季度第一天
  DateTime quarterFirstDay() {
    int m = month;
    if (m <= 3) {
      m = 1;
    } else if (m <= 6) {
      m = 4;
    } else if (m <= 9) {
      m = 7;
    } else {
      m = 10;
    }
    return DateTime(year, m);
  }

  /// 上月第一天
  DateTime previousMonthFirstDay() {
    return DateTime(year, month - 1);
  }

  /// 本月第一天
  DateTime monthFirstDay() {
    return DateTime(year, month);
  }

  /// 本月最后一天
  DateTime monthLastDay() {
    DateTime date = DateTime(year, month).toUtc();
    final day = nextMonthFirstDay().toUtc().difference(date).inDays;
    return DateTime(year, month, day);
  }

  /// 下月第一天
  DateTime nextMonthFirstDay() {
    return DateTime(year, month + 1);
  }

  /// 前一天
  DateTime previousDay() {
    return DateTime(year, month, day - 1);
  }

  /// 后一天
  DateTime nextDay() {
    return DateTime(year, month, day + 1);
  }

  /// 之前的时间
  DateTime before({
    int? hour,
    int? minute,
    int? seconds,
    int? milliseconds,
  }) {
    return DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch -
        (hour ?? 0) * 60 * 60 * 1000 -
        (minute ?? 0) * 60 * 1000 -
        (seconds ?? 0) * 1000 -
        millisecond);
  }

  /// 之后的时间
  DateTime after({
    int? hour,
    int? minute,
    int? seconds,
    int? milliseconds,
  }) {
    return DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch +
        (hour ?? 0) * 60 * 60 * 1000 +
        (minute ?? 0) * 60 * 1000 +
        (seconds ?? 0) * 1000 +
        millisecond);
  }
}
